package com.xmb.wechat.view.wechat.money;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.nil.sdk.utils.DateUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.BankCardBean;
import com.xmb.wechat.definterface.HintListener;
import com.xmb.wechat.definterface.SimpleTextWatcher;
import com.xmb.wechat.entity.withDrawEntity;

import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/15 08:47
 */
public class WechatFundWithdrawActivity extends BaseActivity {

    private static final int REQUEST_BANK_CARD = 717;

    @BindView(R2.id.et_money)
    EditText mEtMoney;
    @BindView(R2.id.tv_deal_time)
    TextView mTvDealTime;
    @BindView(R2.id.switch_charge)
    Switch   mSwitchCharge;
    @BindView(R2.id.tv_bank_card)
    TextView mTvBankCard;
    @BindView(R2.id.tv_tips)
    TextView mTvTips;
    @BindView(R2.id.btn_save)
    Button   mBtnSave;

    private withDrawEntity mWithDrawEntity;

    public WechatFundWithdrawActivity() {
        super(R.layout.activity_fund_withdraw);
    }

    @Override
    protected void initView() {
        mWithDrawEntity = new withDrawEntity();
        mEtMoney.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChangedFinish(String value) {
                checkMoney(value);
            }
        });
        mSwitchCharge.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mWithDrawEntity.setServiceChargeFree(isChecked);
            }
        });
        String stringByFormat = DateUtils.getStringByFormat(new Date(), "yyyy-MM-dd HH:mm");
        mTvDealTime.setText(stringByFormat);
        mWithDrawEntity.setDealTime(stringByFormat);
        showExemption(new HintListener() {
            @Override
            public void onConfirm() {
                //do nothing
            }

            @Override
            public void onCancel() {
                WechatFundWithdrawActivity.this.finish();
            }
        });
    }

    @OnClick({R2.id.btn_save, R2.id.re_deal_time, R2.id.re_bank_card})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.btn_save) {
            WechatFundWithdrawDetailActivity.start(WechatFundWithdrawActivity.this, mWithDrawEntity);
        } else if (id == R.id.re_deal_time) {
            KeyboardUtils.hideSoftInput(this);
            @SuppressLint("SimpleDateFormat")
            TimePickerView pvTime = new TimePickerBuilder(this, (date, v) -> {//选中事件回调
                String stringByFormat = DateUtils.getStringByFormat(date, "yyyy-MM-dd HH:mm");
                mTvDealTime.setText(stringByFormat);
                mWithDrawEntity.setDealTime(stringByFormat);
            }).setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                    .setCancelText("取消")//取消按钮文字
                    .setSubmitText("确定")//确认按钮文字
                    .setTitleSize(20)//标题文字大小
                    .setTitleText("时间")//标题文字
                    .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                    .isCyclic(true)//是否循环滚动
                    .setSubmitColor(getResources().getColor(R.color.wechat_tab_bg_green))//确定按钮文字颜色
                    .setCancelColor(getResources().getColor(R.color.wechat_tab_bg_green))//取消按钮文字颜色
                    .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
                    .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                    .build();
            pvTime.show();
        } else if (id == R.id.re_bank_card) {
            WechatFundBankCardActivity.startforResult(this, REQUEST_BANK_CARD);
        }
    }

    private void checkMoney(String value) {
        try {
            if (TextUtils.isEmpty(value)) {
                mBtnSave.setEnabled(false);
                return;
            }
            double m = Double.parseDouble(value);
            if (m < 0.01) {
                mTvTips.setVisibility(View.VISIBLE);
                mTvTips.setText("提现金额不能少于0.01元");
            } else if (m > 50000) {
                mTvTips.setText("单笔提现金额不能超过五万元");
                mTvTips.setVisibility(View.VISIBLE);
            } else {
                mTvTips.setVisibility(View.GONE);
            }
            if (m < 0.01 || m > 50000) {
                mBtnSave.setEnabled(false);
                return;
            }
            mWithDrawEntity.setMoney(m);
            checkIsAllSatisfy();
        } catch (Exception e) {
            ToastUtils.showShort("金额输入有误");
        }
    }

    /**
     * 检查各条件是否具备(按钮是否可置able)
     */
    private void checkIsAllSatisfy() {
        String money = mEtMoney.getText().toString().trim();
        if (TextUtils.isEmpty(money))
            return;
        String bankCard = mTvBankCard.getText().toString().trim();
        if (TextUtils.isEmpty(bankCard) || bankCard.equals("请选择"))
            return;
        String dealTime = mTvDealTime.getText().toString().trim();
        if (TextUtils.isEmpty(dealTime) || dealTime.equals("请选择"))
            return;
        mBtnSave.setEnabled(true);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (data == null)
                return;
            if (requestCode == REQUEST_BANK_CARD) {
                BankCardBean result = (BankCardBean) data.getSerializableExtra("result");
                mTvBankCard.setText(result.getBankName() + "(" + result.getBankNumber() + ")");
                mWithDrawEntity.setBanCard(result.getBankName() + "  尾号" + result.getBankNumber());
            }
            checkIsAllSatisfy();
        }
    }
}
